//var luid=localStorage.getItem("luid");
//var ulevel=localStorage.getItem("ulevel");
//var ucoin=localStorage.getItem("ucoin");
//var vip=localStorage.getItem("vip");
//var uname=localStorage.getItem("uname");

function ShopScene(){
    var self=this;
    this.$me = $("<div id='equip'></div>");
    //车手的列表
    this.$equipren = $("<div id='equipren'></div>");
    var $renmain=$("<div id='renmain'></div>");
    //人
    var $ren=$("<div class='biker' id='biker'></div>");
    $ren.css("background","url(images/biker1.png)");
    //车
    var $motuo=$("<div class='bike' id='bike'></div>");
    $motuo.css("background-image","url(images/bike11.png)");
    //轮胎
    var $luntai1=$("<div class='wheel lwhell' id='lwhell'></div>");
    $luntai1.css("background-image","url(images/lun1.png)");
    var $luntai2=$("<div class='wheel rwhell' id='rwhell'></div>");
    $luntai2.css("background-image","url(images/lun1.png)");
      self.$equiprenzi = $("<div id='equiprenzi'>User:"+uname+"<br>等级:<span class='equipspan'>"+ulevel+
        "</span>级<br>金币:<span class='equipspan'>"+ucoin+"</span></div>");
      self.$equipren.append(self.$equiprenzi);

    this.$me.append(this.$equipren);
    
    this.$equipren.append($renmain);
    
    $renmain.append($ren);
    $renmain.append($motuo);
    $renmain.append($luntai1);
    $renmain.append($luntai2);
    //装备栏
    this.$equiplan   = $("<div class='zhuangbei'></div>"); 
    this.$zhbeul  = $("<ul class='zbul'></ul>");
    //this.$zhbeli1 = $("<li><a id='zhbeli1' class='zhbeli' href='#'>我的装备</a></li>");
    this.$zhbeli2 = $("<li><a id='zhbeli2' class='zhbeli' href='#'>车手</a></li>");
    this.$zhbeli3 = $("<li><a id='zhbeli3' class='zhbeli' href='#'>摩托</a></li>");
    this.$zhbeli4 = $("<li><a id='zhbeli4' class='zhbeli' href='#'>轮胎</a></li>");
    this.$zhbetable=$("<div class='zhbetable'></div>")
    //this.$zhbeul.append(this.$zhbeli1);
    this.$zhbeul.append(this.$zhbeli2);
    this.$zhbeul.append(this.$zhbeli3);
    this.$zhbeul.append(this.$zhbeli4);
    this.$equiplan.append(this.$zhbeul);
    this.$equiplan.append(this.$zhbetable);
    this.$me.append(this.$equiplan);
//************************************创建车手列表*******************************
this.loginbiker=function (){
  var jsonobj={"handleid":14,"userid":luid};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
           var rs=[];
           for(var prop in val){
             rs.push(val[prop]);
           }
           rs_arr.push(rs);
          console.log(rs_arr);
         });
         //错误的情况
         if(rs_arr[0][0]=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(rs_arr[0][0]!=='error'){
           //循环创建列表

         for(var i=0;i<rs_arr[0].length;i++){
          //console.log(rs_arr[0][i]['ri'])
             var creatab=new Creatable(rs_arr[0][i]['rp'],rs_arr[0][i]['rn'],rs_arr[0][i]['ri']);
             self.$zhbetable.append(creatab.$zbdiv);
             if(rs_arr[0][i]['riderid']==null){
                creatab.$zbdiv.append(creatab.$zbtr3);
                creatab.yulan($("#biker"));
                creatab.goumai1(rs_arr[0][i]['rp'],self.$me,rs_arr[0][i]['rid'],rs_arr[0][i]['rn']);
                //console.log("111111111111"+rs_arr[0][i]['rid'])
             }
             else{
                creatab.$zbdiv.append(creatab.$zbtr4);
             }
           }
         } 
       },
        error:function(responexml,name){
          alert("sche"+name);
           }
  });
}
                     
      this.loginbiker();
//***************************创建摩托列表******************************
 this.loginbike=function (){
    var jsonobj={"handleid":15,"userid":luid};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
           var rs=[];
           for(var prop in val){
             rs.push(val[prop]);
           }
           rs_arr.push(rs);
          console.log(rs_arr);
         });
         //错误的情况
         if(rs_arr[0][0]=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(rs_arr[0][0]!=='error'){
           //循环创建列表

         for(var i=0;i<rs_arr[0].length;i++){
          console.log(rs_arr[0][i]['ri'])
             var creatab=new Creatable(rs_arr[0][i]['rp'],rs_arr[0][i]['rn'],rs_arr[0][i]['ri']);
             self.$zhbetable.append(creatab.$zbdiv);
             if(rs_arr[0][i]['bikeid']==null){
                creatab.$zbdiv.append(creatab.$zbtr3);
                creatab.yulan($("#bike"));
                creatab.goumai2(rs_arr[0][i]['rp'],self.$me,rs_arr[0][i]['rid'],rs_arr[0][i]['rn']);
             }
             else{
                creatab.$zbdiv.append(creatab.$zbtr4);
             }
           }
         } 
       },
        error:function(responexml,name){
          alert("sche"+name);
           }
  });
 }
 //*********************************创建装备列表*******************************
 this.loginwheel=function (){
   var jsonobj={"handleid":16,"userid":luid};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
           var rs=[];
           for(var prop in val){
             rs.push(val[prop]);
           }
           rs_arr.push(rs);
          console.log(rs_arr);
         });
         //错误的情况
         if(rs_arr[0][0]=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(rs_arr[0][0]!=='error'){
           //循环创建列表
         for(var i=0;i<rs_arr[0].length;i++){
          console.log(rs_arr[0][i]['ri'])
             var creatab=new Creatable(rs_arr[0][i]['rp'],rs_arr[0][i]['rn'],rs_arr[0][i]['ri']);
             self.$zhbetable.append(creatab.$zbdiv);
             if(rs_arr[0][i]['equipid']==null){
                creatab.$zbdiv.append(creatab.$zbtr3);
                creatab.yulan($(".wheel"));
                creatab.goumai3(rs_arr[0][i]['rp'],self.$me,rs_arr[0][i]['rid'],rs_arr[0][i]['rn']);

             }
             else{
                creatab.$zbdiv.append(creatab.$zbtr4);
             }
           }
         } 
       },
        error:function(responexml,name){
          alert("sche"+name);
           }
  });
 }
    //点击车手
    this.$zhbeli2.on("click", function(){
        $("#zhbeli2").css("background-color","#73D4CB");
        $("#zhbeli3").css("background-color","#BCE6E2");
        $("#zhbeli4").css("background-color","#BCE6E2");
        self.$zhbetable.empty();
        self.loginbiker();

    });
    //点击摩托
    this.$zhbeli3.on("click", function(){
        $("#zhbeli3").css("background-color","#73D4CB");
        $("#zhbeli2").css("background-color","#BCE6E2");
        $("#zhbeli4").css("background-color","#BCE6E2");
        self.$zhbetable.empty();
        self.loginbike();
    });
    //点击车轮
    this.$zhbeli4.on("click", function(){
        $("#zhbeli4").css("background-color","#73D4CB");
        $("#zhbeli2").css("background-color","#BCE6E2");
        $("#zhbeli3").css("background-color","#BCE6E2");
        self.$zhbetable.empty();
        self.loginwheel();
    });
    
    
    //退出按钮
    this.$but=$("<a href='#' id='retur'  class='retur' ></a>");
    this.$me.append(this.$but);
    this.$but.click(function(){
        director.runScene(new MainScene());
    });


}

function Creatable(money,name,img){
    this.$zbdiv=$("<div class='zbtab'></div>");
    var $zbtr1=$("<button class='zbtabtr1' style='background: url("+img+") no-repeat;'></button>");
    var $zbtr2=$("<div class='zbtabtr2' style='height: 40px;'>"+name+"<br>金币:"+money+"</div>");
    this.$zbtr3=$("<div class='zbtabtr2'><button name='goumai' style='background-color:#2EBD75;'>购买</button>&nbsp;&nbsp;<button name='yulan' style='background-color:#2EBD75;'>预览</button></div>");
    this.$zbtr4=$("<div class='zbtabtr2'><button style='background-color:#2EBD75;'>已拥有</button></button></div>");
    this.$zbdiv.append($zbtr1);
    this.$zbdiv.append($zbtr2);
    //this.$zbdiv.append(this.$zbtr3);

    this.yulan=function(part){
        this.$zbtr3.on("click","button[name='yulan']",  function(){
                part.css("background-image","url("+img+")");
            });
        }
    this.goumai1=function(price,part,rid,rn){
    // console.log(rid);
        this.$zbtr3.on("click","button[name='goumai']",  function(){
            var $wan=$("<div class='wan' style='padding-top:65px;text-align:center;background-repeat:no-repeat'>购买此物需花费<br>"+price+"金币<br><button name='wangoumai' style='background-color:#2EBD75;'>确定</button></div>");
            var $wanq=$("<div class='wanq'></div>");
            $wan.fadeIn(300 ,function(){
                   $wan.animate({top:"40%"});
                   $wanq.animate({top:"38%"});
               });
            part.append($wan);
            part.append($wanq);
            $wanq.click(function(){
               $wan.fadeOut(10);
               $wanq.fadeOut(10);
            });
            $wan.on("click","button[name='wangoumai']",function(){
    
    if ((ucoin-price)>=0){
     ucoin=ucoin-price;
    var jsonobj={"handleid":18,"userid":luid,"riderid":rid,"ucoin":ucoin,"rname":rn};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
         });
         //错误的情况
         if(data['rs']=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(data['rs']!=='error'){
          alert("购买成功");
          director.runScene(new ShopScene());
           }
       },
        error:function(responexml,name){
          alert("bike"+name);
           } 
        });
   }
   else{
    alert("金币不足");
   }
  });
});
}

        this.goumai2=function(price,part,rid,rn){
        this.$zbtr3.on("click","button[name='goumai']",  function(){
            var $wan=$("<div class='wan' style='padding-top:65px;text-align:center;background-repeat:no-repeat'>购买此物需花费<br>"+price+"金币<br><button name='wangoumai' style='background-color:#2EBD75;'>确定</button></div>");
            var $wanq=$("<div class='wanq'></div>");
            $wan.fadeIn(300 ,function(){
                   $wan.animate({top:"40%"});
                   $wanq.animate({top:"38%"});
               });
            part.append($wan);
            part.append($wanq);
            $wanq.click(function(){
               $wan.fadeOut(10);
               $wanq.fadeOut(10);
            });
            $wan.on("click","button[name='wangoumai']",  function(){
    if ((ucoin-price)>0){
     ucoin=ucoin-price;
    var jsonobj={"handleid":19,"userid":luid,"riderid":rid,"ucoin":ucoin,"rname":rn};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
         });
         //错误的情况
         if(data['rs']=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(data['rs']!=='error'){
          alert("购买成功");
          director.runScene(new ShopScene());
           }
       },
        error:function(responexml,name){
          alert("bike"+name);
           } 
        });
   }
   else{
    alert("金币不足");
   }
        });
        });
        }
        this.goumai3=function(price,part,rid,rn){
        this.$zbtr3.on("click","button[name='goumai']",  function(){
            var $wan=$("<div class='wan' style='padding-top:65px;text-align:center;background-repeat:no-repeat'>购买此物需花费<br>"+price+"金币<br><button name='wangoumai' style='background-color:#2EBD75;'>确定</button></div>");
            var $wanq=$("<div class='wanq'></div>");
            $wan.fadeIn(300 ,function(){
                   $wan.animate({top:"40%"});
                   $wanq.animate({top:"38%"});
               });
            part.append($wan);
            part.append($wanq);
            $wanq.click(function(){
               $wan.fadeOut(10);
               $wanq.fadeOut(10);
            });
            $wan.on("click","button[name='wangoumai']",  function(){
             if ((ucoin-price)>0){
     ucoin=ucoin-price;
    var jsonobj={"handleid":20,"userid":luid,"riderid":rid,"ucoin":ucoin,"rname":rn};
     $.ajax({
       type:"post",
       url:"handle/handout.php",
       dataType:"json",
       data:jsonobj,
       success:function(data){
        //alert(data);
         var rs_arr=[];
         $(data).each(function(i,val){
         });
         //错误的情况
         if(data['rs']=='error'){                      
         alert(data['errorinf']);
         }     
         //正确的情况
         if(data['rs']!=='error'){
          alert("购买成功");
          director.runScene(new ShopScene());
           }
       },
        error:function(responexml,name){
          alert("bike"+name);
           } 
        });
   }
   else{
    alert("金币不足");
   }
        });
        });
        }
      }
    
